//
//  YPTenderViewController.m
//  ChengHuiTong
//
//  Created by xzm on 16/11/25.
//  Copyright © 2016年 张文权. All rights reserved.
//


#import "YPTenderViewController.h"
#import "TPKeyboardAvoidingTableView.h"
#import "YPTenderModel.h"
#import "YPChoiceRedPacketViewController.h"
#import "MoneyTextField.h"

@interface YPTenderViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>{
    
    NSString *_lastInputMoney; //上次输入的红包
    RedbagModel *_rbSelectModel; //选中红包的模型
    RedbagModel *_cpSelectModel; //选中加息劵的模型
    double _finalInvestMoney;

}

@property (nonatomic,weak)UILabel *tenderMoneyLabel; //可投金额
@property (nonatomic,weak)UILabel *accountMoneyLabel; //账户余额
@property (nonatomic,strong)YPTenderModel *tenderModel; //数据模型
@property (nonatomic,strong)TPKeyboardAvoidingTableView *tableView;
@property (nonatomic,strong)UILabel *profitLabel; //预期收益
@property (nonatomic,strong)UILabel *redBagLabel; //使用红包
@property (nonatomic,strong)UILabel *couponLabel; //使用加息劵
@property (nonatomic,strong)UILabel *rbNumLabel; //可使用红包数
@property (nonatomic,strong)UILabel *cpNumLabel; //可使用加息劵数
@property (nonatomic,strong)UILabel *redBagMoneyLabel; //使用红包抵扣金额
@property (nonatomic,strong)UILabel *finalPayLabel; //实际支付金额
@property (nonatomic,strong)MoneyTextField *moneyTF; //投资金额
@property (nonatomic,strong)UITextField *psdTF; //定向密码

@end

const CGFloat tableViewRowHeight = 45;

@implementation YPTenderViewController

#pragma mark -- lazzy
- (TPKeyboardAvoidingTableView *)tableView{
    
    if (!_tableView) {
        _tableView = [[TPKeyboardAvoidingTableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView setSeparatorInset:UIEdgeInsetsMake(0, 15, 0, 0)];
        _tableView.separatorColor = UIColorFromString(@"e6e6e6");
        _tableView.backgroundColor = kColor_AppBaseBackground;
        _tableView.tableHeaderView = [self tableViewHeadView];
        _tableView.tableFooterView = [self tableViewFootView];
    }
    
    return _tableView;
}

- (MoneyTextField *)moneyTF{
    
    if (!_moneyTF) {
        _moneyTF = [[MoneyTextField alloc]initWithFrame:CGRectMake(20, 0, 200, tableViewRowHeight)];
        _moneyTF.placeholder = [NSString stringWithFormat:@"起投金额%@元",self.tenderModel.lowest_account == nil ? @"0":self.tenderModel.lowest_account];
        _moneyTF.font = [UIFont systemFontOfSize:14];
        [_moneyTF addTarget:self action:@selector(moneyChange:) forControlEvents:UIControlEventEditingChanged];
    }
    return _moneyTF;
}

- (UITextField *)psdTF{
    
    if (!_psdTF) {
        _psdTF = [[UITextField alloc]initWithFrame:CGRectMake(20, 0, SCREEN_WIDTH - 40, tableViewRowHeight)];
        _psdTF.placeholder = @"请输入定向密码";
        _psdTF.font = [UIFont systemFontOfSize:14];
        _psdTF.secureTextEntry = YES;
    }
    return _psdTF;
}

- (UILabel *)profitLabel{
    
    if (!_profitLabel) {
        _profitLabel = [UILabel creatWithFrame:CGRectMake(20, 0, 300, tableViewRowHeight)
                                     font:[UIFont systemFontOfSize:14]
                                     text:@""
                                textColor:[UIColor darkGrayColor]
                            textAlignment:0];
        NSMutableAttributedString*string=[[NSMutableAttributedString alloc] initWithString:@"预计收益 : 0.00元"];
        [string addAttribute:NSForegroundColorAttributeName value:kColor_AppNavColor range:NSMakeRange(7, 4)];
        _profitLabel.attributedText = string;
    }
    
    return _profitLabel;
}

- (UILabel *)redBagLabel{
    
    if (!_redBagLabel) {
        _redBagLabel = [UILabel creatWithFrame:CGRectMake(20, 0, 200, tableViewRowHeight)
                                          font:[UIFont systemFontOfSize:14]
                                          text:self.tenderModel.redbag.count > 0? @"使用红包":@"暂无可用红包"
                                     textColor:[UIColor lightGrayColor]
                                 textAlignment:0];
    }
    
    return _redBagLabel;
}

- (UILabel *)couponLabel{
    
    if (!_couponLabel) {
        _couponLabel = [UILabel creatWithFrame:CGRectMake(20, 0, 200, tableViewRowHeight)
                                          font:[UIFont systemFontOfSize:14]
                                          text:self.tenderModel.aprcoupon.count > 0?@"使用加息劵":@"暂无可用加息劵"
                                     textColor:[UIColor lightGrayColor]
                                 textAlignment:0];
    }
    return _couponLabel;
}

- (UILabel *)rbNumLabel{
    
    if (!_rbNumLabel) {
        _rbNumLabel = [UILabel creatWithFrame:CGRectMake(SCREEN_WIDTH - 60 - autoScaleW(15) - 20, (tableViewRowHeight - 20)/2.f, 60, 20)
                                         font:[UIFont systemFontOfSize:12]
                                         text:@""
                                    textColor:[UIColor whiteColor]
                                textAlignment:1];
        _rbNumLabel.backgroundColor = kColor_AppNavColor;
        _rbNumLabel.layer.cornerRadius = 4.f;
        _rbNumLabel.layer.masksToBounds = YES;
        _rbNumLabel.hidden = YES;
    }
    return _rbNumLabel;
}

- (UILabel *)cpNumLabel{
    
    if (!_cpNumLabel) {
        _cpNumLabel = [UILabel creatWithFrame:self.rbNumLabel.frame
                                         font:self.rbNumLabel.font
                                         text:@""
                                    textColor:[UIColor whiteColor]
                                textAlignment:1];
        _cpNumLabel.backgroundColor = kColor_AppNavColor;
        _cpNumLabel.layer.cornerRadius = self.rbNumLabel.layer.cornerRadius;
        _cpNumLabel.layer.masksToBounds = YES;
        _cpNumLabel.hidden = YES;
    }
    return _cpNumLabel;
}

- (UILabel *)redBagMoneyLabel{
    
    if (!_redBagMoneyLabel) {
        _redBagMoneyLabel = [UILabel creatWithFrame:CGRectMake(20, 45, SCREEN_WIDTH - 40, 35)
                                               font:[UIFont systemFontOfSize:14]
                                               text:@""
                                          textColor:[UIColor darkGrayColor]
                                      textAlignment:0];
        NSMutableAttributedString*string=[[NSMutableAttributedString alloc] initWithString:@"红包抵扣 : 0.00元"];
        [string addAttribute:NSForegroundColorAttributeName value:kColor_AppNavColor range:NSMakeRange(7, 4)];
        _redBagMoneyLabel.attributedText = string;
    }
    
    return _redBagMoneyLabel;
}

- (UILabel *)finalPayLabel{
    
    if (!_finalPayLabel) {
        _finalPayLabel = [UILabel creatWithFrame:CGRectMake(20, self.redBagMoneyLabel.bottom, SCREEN_WIDTH - 40, 35)
                                            font:[UIFont systemFontOfSize:14]
                                            text:@""
                                       textColor:[UIColor darkGrayColor]
                                   textAlignment:0];
        NSMutableAttributedString*string=[[NSMutableAttributedString alloc] initWithString:@"实际支付 : 0.00元"];
        [string addAttribute:NSForegroundColorAttributeName value:kColor_AppNavColor range:NSMakeRange(7, 4)];
        _finalPayLabel.attributedText = string;
    }
    return _finalPayLabel;
}

- (UIView *)tableViewHeadView{
    
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 130)];
    
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, -300, SCREEN_WIDTH, 450)];
    bgView.backgroundColor = kColor_AppNavColor;
    [headView addSubview:bgView];
    
    //可投金额
    UILabel *tenderLabel = [UILabel creatWithFrame:CGRectMake(20, 20, 105, 35)
                                              font:[UIFont systemFontOfSize:15]
                                              text:@"可投金额(元) :"
                                         textColor:[UIColor whiteColor]
                                     textAlignment:0];
    [headView addSubview:tenderLabel];
    
    UILabel *tenderMoneyLabel = [UILabel creatWithFrame:CGRectMake(tenderLabel.right, tenderLabel.top, SCREEN_WIDTH - tenderLabel.right - 10, tenderLabel.height)
                                              font:[UIFont systemFontOfSize:18]
                                              text:self.tenderModel.surplus
                                         textColor:[UIColor whiteColor]
                                     textAlignment:0];
    self.tenderMoneyLabel = tenderMoneyLabel;
    [headView addSubview:tenderMoneyLabel];
    
    [headView addLineWithFrame:CGRectMake(10, tenderMoneyLabel.bottom+10, SCREEN_WIDTH - 20, 1) color:[UIColor whiteColor]];
    
    //账户余额
    UILabel *accountLabel = [UILabel creatWithFrame:CGRectMake(tenderLabel.left, tenderLabel.bottom + 20, tenderLabel.width, tenderLabel.height)
                                              font:tenderLabel.font
                                              text:@"账户余额(元) : "
                                         textColor:[UIColor whiteColor]
                                     textAlignment:0];
    [headView addSubview:accountLabel];
    
    UILabel *accountMoneyLabel = [UILabel creatWithFrame:CGRectMake(tenderMoneyLabel.left, accountLabel.top, tenderMoneyLabel.width, accountLabel.height)
                                                   font:[UIFont systemFontOfSize:18]
                                                   text:self.tenderModel.money_usable
                                              textColor:[UIColor whiteColor]
                                          textAlignment:0];
    self.accountMoneyLabel = accountMoneyLabel;
    [headView addSubview:accountMoneyLabel];
    
    
    UIButton *rechargeBtn = [UIButton creatWithFrame:CGRectMake(SCREEN_WIDTH - 75, accountLabel.top + (accountLabel.height - 30)/2.f, 55, 30)
                                                font:[UIFont systemFontOfSize:15]
                                                text:@"充值"
                                              action:@selector(rechargeClick)
                                              target:self];
    rechargeBtn.layer.cornerRadius = 5;
    [rechargeBtn setBorderWithColor:[UIColor whiteColor] borderWidth:1];
    [headView addSubview:rechargeBtn];
    
    
    return headView;
}

- (UIView *)tableViewFootView{
    
    UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100)];
    footView.backgroundColor = [UIColor clearColor];
    
    
    UIButton *tenderBtn = [UIButton creatWithFrame:CGRectMake(50, 20, footView.width - 100, 40)
                                              font:[UIFont systemFontOfSize:18]
                                              text:@"立即投资"
                                            action:@selector(tenderClick)
                                            target:self];
    tenderBtn.layer.cornerRadius = 5;
    tenderBtn.backgroundColor = kColor_AppNavColor;
    [footView addSubview:tenderBtn];
    
    UILabel *remindLabel = [UILabel creatWithFrame:CGRectMake(0, tenderBtn.bottom, SCREEN_WIDTH, 40)
                                     font:[UIFont systemFontOfSize:14]
                                     text:@"*如您有未使用加息劵或红包，请记得使用!*"
                                textColor:kColor_AppNavColor
                            textAlignment:1];
    [footView addSubview:remindLabel];
    
    
    return footView;
}

#pragma mark -- UI
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"投标";
    
}

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    [self setUpData];
}

- (void)setUpData{
    
    YPTenderBin * bin = [[YPTenderBin alloc]init];
    bin.token = TOKEN;
    bin.bid = self.investDetailModel.bid;
    [YPHttpRequestMethod sendAsyRequestURL:CMD_TENDER withData:[bin dictionaryValue] withCompletionBlock:^(NSDictionary *responseDictionary) {
        NSLog(@"responseDictionary-----%@",responseDictionary);
        
        if ([responseDictionary[@"status"] intValue]==0) {
            
            self.tenderModel = [[YPTenderModel alloc] initWithDictionary:responseDictionary];

            if (self.tenderModel.hs_flag.integerValue == 1) {
                
                [self setUpUI];
                
            }else{
                [self showHsBoxWithFlag:self.tenderModel.hs_flag.integerValue];
            }
            
        }
        
    } withFailedBlock:^(NSError *error) {
        
    } withIndicator:self.view.subviews.count > 0?NO:YES atView:self.view];
}

- (void)setUpUI{
    
    if (![self.view.subviews containsObject:self.tableView]) [self.view addSubview:self.tableView];
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section==1) {

        if ([self.moneyTF.text floatValue]>0 || self.tenderModel.lowest_account == nil || self.tenderModel.lowest_account.length == 0) { //有输入金额，或者是从0起投的
            
            if (indexPath.row == 0) {
                
                if (self.tenderModel.redbag.count == 0) {
                    return;
                }
                void (^ rbUnuseBlock)() = ^{
                    self.redBagLabel.text= @"使用红包";
                    self.redBagLabel.textColor = [UIColor lightGrayColor];
                    [self configRedbagAndFinalPayWithRedbagMoney:@"0.00"];
                    self.rbNumLabel.text = [NSString stringWithFormat:@"%ld个可选",[self getCanSelectNumWithDataArr:self.tenderModel.redbag]];
                };
                
                YPChoiceRedPacketViewController*choiceVC=[[YPChoiceRedPacketViewController alloc] init];
                choiceVC.redBagArr=self.tenderModel.redbag;
                choiceVC.min_tender=self.moneyTF.text;
                choiceVC.title = @"选择红包";
                choiceVC.useRedBag=^(RedbagModel*model){
                    
                    _rbSelectModel = model;
                    if (model) {
                        
                        [self configRedbagAndFinalPayWithRedbagMoney:model.money];
                        self.rbNumLabel.text = @"已选1个";
                        
                    }else{
                        rbUnuseBlock();
                    }
                    
                };
                choiceVC.UnuseRedBag = ^{
                    _rbSelectModel = nil;
                    rbUnuseBlock();
                };
                
                [self.navigationController pushViewController:choiceVC animated:YES];
                
            }else{
                
                if (self.tenderModel.aprcoupon.count == 0) {
                    
                    return;
                }
                
                void (^ cpUnuseBlock)() = ^{
                    self.couponLabel.text= @"使用加息劵";
                    self.couponLabel.textColor = [UIColor lightGrayColor];
                    self.profitLabel.attributedText = [self getFinalProfitMoney];
                    self.cpNumLabel.text = [NSString stringWithFormat:@"%ld个可选",[self getCanSelectNumWithDataArr:self.tenderModel.aprcoupon]];
                };
                
                YPChoiceRedPacketViewController*choiceVC=[[YPChoiceRedPacketViewController alloc] init];
                choiceVC.redBagArr=self.tenderModel.aprcoupon;
                choiceVC.title = @"选择加息劵";
                choiceVC.min_tender=self.moneyTF.text;
                choiceVC.useRedBag=^(RedbagModel*model){
                    _cpSelectModel = model;
                    
                    if (model) {
                        NSString *speStr = [NSString stringWithFormat:@"加息%@%%",model.apr];
                        NSString *couponStr = [NSString stringWithFormat:@"使用加息劵 : %@",speStr];
                        self.couponLabel.attributedText = [couponStr getAttributedStringWithSpecialString:speStr speColor:kColor_AppNavColor speFont:[UIFont systemFontOfSize:13]];
                        self.cpNumLabel.text = @"已选1张";
                        
                    }else{
                        cpUnuseBlock();
                        
                    }
                    self.profitLabel.attributedText = [self getFinalProfitMoney];
                    
                };
                choiceVC.UnuseRedBag = ^{
                    _cpSelectModel = nil;
                    cpUnuseBlock();
                };
                
                [self.navigationController pushViewController:choiceVC animated:YES];
            }
            
        }else{
            
            [MBProgressHUD showError:@"请输入有效的投资金额" toView:self.view];
        }
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0 || section ==2) {
        
        return 1;
    }else{
        return 2;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    cell.selectionStyle = 0;
    
    switch (indexPath.section) {
        case 0:{
            
            [cell.contentView addSubview:self.moneyTF];
            
            UIButton *btn = [UIButton creatWithFrame:CGRectMake(SCREEN_WIDTH - 100, (tableViewRowHeight - 27)/2.f, 80, 27)
                                                font:[UIFont systemFontOfSize:14]
                                                text:@"余额全投"
                                           textColor:kColor_AppNavColor
                                              action:@selector(allTenderClick)
                                              target:self];
            btn.layer.cornerRadius = 5;
            [btn setBorderWithColor:kColor_AppNavColor borderWidth:1];
            [cell.contentView addSubview:btn];
        }
            
            break;
        case 1:{

            if (indexPath.row == 0) {
                [cell.contentView addSubview:self.redBagLabel];
                [cell.contentView addSubview:self.rbNumLabel];
                cell.accessoryType = self.tenderModel.redbag.count > 0? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
            }else{
                [cell.contentView addSubview:self.couponLabel];
                [cell.contentView addSubview:self.cpNumLabel];
                cell.accessoryType = self.tenderModel.aprcoupon.count > 0? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
            }
        }
            
            break;
        case 2:{
            
            if ([self.investDetailModel.type intValue] == 4) { //约标，需要输入定向密码
                
                [cell.contentView addSubview:self.psdTF];
            }
            
        }
            
            break;
            
        default:
            break;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 2 && [self.investDetailModel.type intValue] == 3) { //投标没有定向密码
        
        return 0;
    }
    
    return tableViewRowHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return section == 2?100:tableViewRowHeight;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, section == 2?70:tableViewRowHeight)];
    view.backgroundColor = tableView.backgroundColor;
    
    UIColor *textColor = [UIColor darkGrayColor];
    
    switch (section) {
        case 0:{
            
            UILabel *label = [UILabel creatWithFrame:CGRectMake(20, 0, 200, view.height)
                                                font:[UIFont systemFontOfSize:14]
                                                text:@"输入购买金额"
                                           textColor:textColor
                                       textAlignment:0];
            [view addSubview:label];
        }
            
            break;
        case 1:{
            
            [view addSubview:self.profitLabel];
        }
            
            break;
        case 2:{
            
            UILabel *remindLabel = [view viewWithTag:199];
            if (!remindLabel) {
                remindLabel = [UILabel creatWithFrame:CGRectMake(20, 5, SCREEN_WIDTH - 40, 40)
                                                 font:[UIFont systemFontOfSize:14]
                                                 text:@"请先输入金额再选择加息劵，若选择加息劵后再修改投标金额，则需要重新选择加息劵"
                                            textColor:kColor_redNumberColor
                                        textAlignment:0];
                remindLabel.numberOfLines = 0;
                remindLabel.tag = 199;
            }
            [view addSubview:remindLabel];

            [view addSubview:self.redBagMoneyLabel];

            [view addSubview:self.finalPayLabel];
            
        }
            
            break;
            
        default:
            break;
    }
    return view;
}

#pragma mark -- 事件响应
/**充值*/
- (void)rechargeClick{
    
    YPGoWebView(String_Connect(CMD_RECHARGE, @"&token", TOKEN), self.navigationController);
}
/**投资*/
- (void)tenderClick{
    
    if ([AppTools handleNullToString:self.moneyTF.text].length == 0 || _finalInvestMoney <= 0) {
        
        [MBProgressHUD showError:@"请输入有效的投资金额" toView:self.view];
        return;
    }
    if (_finalInvestMoney > [AppTools stringDeleteString:self.tenderMoneyLabel.text].doubleValue) {
        [MBProgressHUD showError:@"投资金额不能大于可投金额" toView:self.view];
        return;
    }
    if (_finalInvestMoney > [AppTools stringDeleteString:self.accountMoneyLabel.text].doubleValue) {
        [MBProgressHUD showError:@"投资金额不能大于账户余额" toView:self.view];
        return;
    }
    
    //三农标
    if ([self.investDetailModel.style isEqualToString:@"5"]) {
        
        CGFloat persent = 10000;
        double investMoney = self.moneyTF.text.doubleValue;
        //判断是不是万的倍数
        BOOL isInt =  [AppTools isPureInt:s_Num(@(investMoney/persent))];
        if ((investMoney < persent || !isInt)) {
            [MBProgressHUD showError:@"该标只支持1万的倍数投资" toView:self.view];
            return;
        }
        
    }
    
    //约标必须输入定向密码
    if ([self.investDetailModel.type intValue] == 4 && [AppTools handleNullToString:self.psdTF.text].length == 0) {
        
        [YRProgressHUD autoShowHUDAddedTo:self.view withText:@"请输入定向密码"];
        return;
    }

    YPMakeSureTenderBin * bin = [[YPMakeSureTenderBin alloc]init];
    bin.token =[YPShareAccountInfo shareAccountInfo].token;
    bin.bid = self.investDetailModel.bid;
    bin.money = self.moneyTF.text;
    bin.pwd = self.psdTF.text;
    bin.redid = _rbSelectModel?_rbSelectModel.redid:@"0";
    bin.couponid = _cpSelectModel?_cpSelectModel.couponid:@"0";

    [YPHttpRequestMethod sendAsyRequestURL:CMD_MAKE_SURE_HOSTING_TENDER withData:[bin dictionaryValue] withCompletionBlock:^(NSDictionary *responseDictionary) {
        
        NSLog(@"responseDictionary%@",responseDictionary);
        
        if ([responseDictionary[@"status"] intValue]==0) {

            //投资成功
            NSString * url= [NSString stringWithFormat:@"%@index.php?m=app&c=hsbank&a=tenderDeal&token=%@&bid=%@&money=%@&showpage=%@",kAppHostAddress,[YPShareAccountInfo shareAccountInfo].token,bin.bid,bin.money,@"1"];
            
            if (![bin.redid isEqualToString:@"0"]) {
                
                url = [url stringByAppendingString:[NSString stringWithFormat:@"&redid=%@",bin.redid]];
                if (![bin.couponid isEqualToString:@"0"]) {
                    
                    url = [url stringByAppendingString:[NSString stringWithFormat:@"&couponid=%@",bin.couponid]];
                }
                
            }else if (![bin.couponid isEqualToString:@"0"]){
                
                url = [url stringByAppendingString:[NSString stringWithFormat:@"&couponid=%@",bin.couponid]];

            }
            if (self.investDetailModel.type.intValue == 4 ) {
                url = [url stringByAppendingString:[NSString stringWithFormat:@"&pwd=%@",self.psdTF.text]];
            }
            
            NSLog(@"url=%@",url);
            YPGoWebView(url, self.navigationController);
            
        }else if ([responseDictionary[@"status"] intValue] ==1202){
            [YRProgressHUD autoShowHUDAddedTo:self.view withText:@"token不匹配"];
            
        }else if ([responseDictionary[@"status"] intValue] ==1303){

            YPGoWebView(String_Connect(CMD_DEPOSITORY, @"&token=", TOKEN), self.navigationController);
            
        }
        
        else if ([responseDictionary[@"status"] intValue] ==1306){
            [YRProgressHUD autoShowHUDAddedTo:self.view withText:@"定向密码错误"];
            
        }else if ([responseDictionary[@"status"] intValue] ==1307){
            
            [YRProgressHUD autoShowHUDAddedTo:self.view withText:responseDictionary[@"error"]];
            
        }
        
        
    } withFailedBlock:^(NSError *error) {
        
        
    } withIndicator:YES atView:self.view];
}
/**余额全投*/
- (void)allTenderClick{
    
    NSString *finalMoney = @"";
    NSString *most = [AppTools stringDeleteString:self.tenderModel.most_account];
    NSString *usable = [AppTools stringDeleteString:self.tenderModel.money_usable];
    NSString *surplus = [AppTools stringDeleteString:self.tenderModel.surplus];
    if (most.doubleValue <= 0) {
        finalMoney = [NSString stringWithFormat:@"%.2f",MIN(usable.doubleValue , surplus.doubleValue)];
    }else{
        finalMoney = [NSString stringWithFormat:@"%.2f",MIN(usable.doubleValue , surplus.doubleValue)];
        finalMoney = [NSString stringWithFormat:@"%.2f",MIN(finalMoney.doubleValue , most.doubleValue)];
    }
    self.moneyTF.text = finalMoney;
    
    [self moneyChange:self.moneyTF];
}
/**投资金额改变*/
- (void)moneyChange:(UITextField *)tf{

    //有加息劵
    if (self.tenderModel.aprcoupon.count > 0) {
        
        if (tf.text.doubleValue != _lastInputMoney.doubleValue) {
            //清空加息劵
            _cpSelectModel = nil;
            self.couponLabel.text = @"使用加息劵";
            self.couponLabel.textColor = [UIColor lightGrayColor];
            
        }
        
        //更换可选加息劵
        self.cpNumLabel.hidden = NO;
        if (!_cpSelectModel) self.cpNumLabel.text = [NSString stringWithFormat:@"%ld张可选",[self getCanSelectNumWithDataArr:self.tenderModel.aprcoupon]];
        
    }

    //有红包
    if (self.tenderModel.redbag.count > 0) {
        
        if (tf.text.doubleValue != _lastInputMoney.doubleValue) {
            //清空红包
            _rbSelectModel = nil;
            [self configRedbagAndFinalPayWithRedbagMoney:@"0.00"];
            
        }
        
        //更换可红包
        self.rbNumLabel.hidden = NO;
        if (!_rbSelectModel) self.rbNumLabel.text = [NSString stringWithFormat:@"%ld个可选",[self getCanSelectNumWithDataArr:self.tenderModel.redbag]];

    }

    self.finalPayLabel.attributedText = [self getFinalPayMoney];
    self.profitLabel.attributedText = [self getFinalProfitMoney];
    _lastInputMoney = tf.text;

    
}
/**配置红包抵扣跟实际支付*/
- (void)configRedbagAndFinalPayWithRedbagMoney:(NSString *)redbagMoney{
    
    if (redbagMoney.floatValue == 0) {
        self.redBagLabel.text=@"使用红包";
        self.redBagLabel.textColor = [UIColor lightGrayColor];
    }else{
        NSString *redBagStr = [NSString stringWithFormat:@"使用红包 : %@元",redbagMoney];
        self.redBagLabel.attributedText = [redBagStr getAttributedStringWithSpecialString:[NSString stringWithFormat:@"%@元",redbagMoney]
                                                                                 speColor:kColor_AppNavColor
                                                                                  speFont:nil];
        
    }
    
    NSString *redBagStr = [NSString stringWithFormat:@"红包抵扣 : %@元",redbagMoney];
    self.redBagMoneyLabel.attributedText = [redBagStr getAttributedStringWithSpecialString:redbagMoney speColor:kColor_AppNavColor speFont:nil];
    self.finalPayLabel.attributedText = [self getFinalPayMoney];
    
}
/**获取实际支付金额*/
- (NSAttributedString *)getFinalPayMoney{
    
    double final = self.moneyTF.text.doubleValue - _rbSelectModel.money.doubleValue;
    final = MAX(final, 0);
    _finalInvestMoney = final;
    NSString *finalStr = [NSString stringWithFormat:@"%.2f",final];
    NSString *finalPay = [NSString stringWithFormat:@"实际支付 : %@元",finalStr];
    NSMutableAttributedString*string=[[NSMutableAttributedString alloc] initWithString:finalPay];
    [string addAttribute:NSForegroundColorAttributeName value:kColor_AppNavColor range:[finalPay rangeOfString:finalStr]];
    return string;
}
/**获取预期收益*/
- (NSAttributedString *)getFinalProfitMoney{
    
    NSMutableAttributedString *finalAttring;
    
    NSString *finalProfit = [AppTools getFinalProfitWithEnterAmount:[self.moneyTF.text doubleValue]
                                                                apr:self.investDetailModel.apr
                                                          timeLimit:self.investDetailModel.time_limit
                                                              style:self.investDetailModel.style
                                                        is_datetype:self.investDetailModel.is_datetype];
    
    
    if (_cpSelectModel.apr.floatValue > 0) { //使用加息劵
        
        NSString *couponProfit = [AppTools getFinalProfitWithEnterAmount:[self.moneyTF.text doubleValue]
                                                                     apr:_cpSelectModel.apr
                                                               timeLimit:self.investDetailModel.time_limit
                                                                   style:self.investDetailModel.style
                                                             is_datetype:self.investDetailModel.is_datetype];
        
        finalProfit = [NSString stringWithFormat:@"%.2f",[finalProfit doubleValue] + [couponProfit doubleValue]];
        
        NSString *couponString = [NSString stringWithFormat:@"(含加息收益%@元)",couponProfit];
        finalAttring = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"预计收益 : %@元%@",finalProfit,couponString]];
        [finalAttring addAttribute:NSForegroundColorAttributeName value:kColor_AppNavColor range:[finalAttring.string rangeOfString:couponProfit]];
        [finalAttring addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:11] range:[finalAttring.string rangeOfString:couponString]];
        
    }else{
       
        finalAttring = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"预计收益 : %@元",finalProfit]];
    }

    

    [finalAttring addAttribute:NSForegroundColorAttributeName value:kColor_AppNavColor range:[finalAttring.string rangeOfString:finalProfit]];

    return finalAttring;
}
/**获取当前可用的红包或加息劵*/
- (NSInteger)getCanSelectNumWithDataArr:(NSArray *)dataArr{
    
    NSInteger count = 0;
    for (NSDictionary*dic in dataArr) {
        
        if (RBCP_CANUSE(self.moneyTF.text, dic[@"min_tender"], dic[@"max_tender"])) {
            count ++;
        }
    }
    
    return count;

}

@end
